"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : PROCEDURE
Case Name   : 创建带有replace into语句的存储过程
Create At   : 2023/01/09
Owner       : @zou_jialiang050
Description :
    1.建B库
    2.B库中建表
    3.B库中创建带有replace into语句的存储过程
    4.调用存储过程
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
History     :
"""

import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Procedure(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.com_sh = CommonSH()
        self.constant = Constant()
        self.table = 't_replace_into_0001'
        self.database = 'd_replace_into_0001'
        self.procedure = 'p_replace_into_0001'

    def test_procedure(self):
        text = '-----step1:建B库 expect:成功-----'
        sql_cmd = f"drop database if exists {self.database};create database " \
            f"{self.database} dbcompatibility = 'B';"
        self.log.info(text)
        msg = self.com_sh.execut_db_sql(sql_cmd)
        self.log.info(msg)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, msg, '执行失败' +
                      text)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, msg, '执行失败' +
                      text)

        text = '-----step2:B库中建表 expect:成功-----'
        sql_cmd = f"drop table if exists {self.table};create table " \
            f"{self.table}(c_int int,c_text text);"
        self.log.info(text)
        msg = self.com_sh.execut_db_sql(sql_cmd, dbname=self.database)
        self.log.info(msg)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, msg, '执行失败' +
                      text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败' +
                      text)

        text = '-----step3:B库中创建带有replace into语句的存储过程 ' \
               'expect:成功-----'
        sql_cmd = f"create or replace procedure {self.procedure}() as " \
            f"begin " \
            f"  replace into {self.table} values(1,'ccccc');" \
            f"end;"
        self.log.info(text)
        msg = self.com_sh.execut_db_sql(sql_cmd, dbname=self.database)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_PROCEDURE_SUCCESS_MSG, msg,
                      '执行失败' + text)

        text = '-----step4:调用存储过程 expect:成功-----'
        sql_cmd = f"call {self.procedure};select * from {self.table};"
        self.log.info(text)
        msg = self.com_sh.execut_db_sql(sql_cmd, dbname=self.database)
        self.log.info(msg)
        self.assertIn('1 | ccccc', msg, '执行失败' + text)

    def tearDown(self):
        text = '-----step5:清理环境 expect:成功-----'
        self.log.info(text)
        cmd = f'drop table {self.table};drop procedure {self.procedure};'
        msg1 = self.com_sh.execut_db_sql(cmd, dbname=self.database)
        self.log.info(msg1)
        cmd = f'drop database {self.database};'
        msg2 = self.com_sh.execut_db_sql(cmd)
        self.log.info(msg2)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, msg1, '执行失败' +
                      text)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, msg2, '执行失败' +
                      text)
        self.assertIn(self.constant.DROP_PROCEDURE_SUCCESS_MSG, msg1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
